/* eslint-disable no-unused-vars */

// 这里的 函数 都是 不发送网络请求的

const UserInfos = {
  admin: {
    session_id: 'admin-token',
    mobile: '13912345678',
    email: 'admin@xxx.com',
    name: 'admin',
    uid: 2,
    partner_id: 3
  },

  user1: {
    session_id: 'user1-token',
    mobile: '13912345678',
    name: 'user1',
    email: 'user1@xxx.com',
    uid: 12,
    partner_id: 13
  }
}

// 登录成功后, 本地存储 user info
const UserInfoKey = 'UserInfo'

const my_return = res => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, 100, res)
  })
}

function random(min, max) {
  return Math.floor(Math.random() * (1 + max - min)) + min
}

export class RPC {
  email_send(payload) {
    console.log('xxx, email_send', payload)
    // const res = await odoo.rpc.sms_send(payload)
    return my_return(true)
  }

  email_back({ mobile }) {
    console.log('xxx, email_back')
    // const code = await odoo.rpc.sms_back(payload)

    const code = `${random(1000, 9999)}`
    return my_return(code)
  }

  sms_send(payload) {
    console.log('xxx, sms_send', payload)
    // const res = await odoo.rpc.sms_send(payload)
    return my_return(true)
  }

  sms_back({ mobile }) {
    console.log('xxx, sms_back')
    // const code = await odoo.rpc.sms_back(payload)
    const code = '1234'
    return my_return(code)
  }

  register({ mobile, code, username, password }) {
    console.log('xxx, register')
    // const response = await odoo.rpc.register(payload)
    const res = {}
    return my_return(res)
  }

  sms_reset_password({ mobile, code, password }) {
    console.log('xxx, sms_reset_password')
    // const response = await odoo.rpc.sms_reset_password(payload)
    const res = true
    return my_return(res)
  }

  async sms_login({ mobile, code }) {
    console.log('xxx, sms_login')
    // const response = await odoo.rpc.sms_login(payload)
    const userinfo = UserInfos.admin
    return this.after_login(userinfo)
  }

  login(params) {
    console.log('odoorpc.user.login:', params)
    const { username, password } = params
    const userinfo = UserInfos[username]
    return this.after_login(userinfo)
  }

  after_login(userinfo) {
    if (!userinfo) {
      return my_return({
        code: 20001,
        data: {}
      })
    }

    localStorage.setItem(UserInfoKey, JSON.stringify(userinfo))
    const res = {
      code: 20000,
      data: { token: userinfo.session_id }
    }

    return my_return(res)
  }

  getInfo(token) {
    console.log('xxx, getInfo', token)
    const userinfo = JSON.parse(localStorage.getItem(UserInfoKey))

    const res = {
      code: 20000,
      data: {
        roles: ['admin'],
        // introduction: 'I am a super administrator',
        // avatar:
        //   'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
        name: userinfo.name
      }
    }

    return my_return(res)
  }

  logout(token) {
    console.log('xxx, logout', token)
    localStorage.clear(UserInfoKey)
    return my_return(true)
  }

  get_userinfo() {
    const userstr = localStorage.getItem(UserInfoKey)
    if (userstr) {
      const userinfo = JSON.parse(localStorage.getItem(UserInfoKey))
      return userinfo
    } else {
      return {}
    }
  }
}

const rpcCreator = params => {
  return new RPC(params)
}

export default rpcCreator
